package cn.com.homeyoda.aimate.aihealth.ui.record.physical_exam;

import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.DELETE_REPORT;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.FACE_ID;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.USER_ID;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jeremyliao.liveeventbus.LiveEventBus;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseFragment;
import cn.com.homeyoda.aimate.aihealth.bean.ExaminationReportBean;
import cn.com.homeyoda.aimate.aihealth.databinding.FragmentPhysicalExamRecordBinding;
import cn.com.homeyoda.aimate.aihealth.ui.my.family.FamilyViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.videoinspect.DetectionResultActivity;
import cn.com.homeyoda.aimate.aihealth.views.CenterDialog;

/**
 * 健康档案
 */
public class PhysicalExamRecordFragment extends BaseFragment<FragmentPhysicalExamRecordBinding, FamilyViewModel> {

    private String faceId;
    private String userId;
    private PhysicalExamRecordAdapter physicalExamRecordAdapter;

    public static PhysicalExamRecordFragment newInstance(String userId, String faceId) {
        PhysicalExamRecordFragment fragment = new PhysicalExamRecordFragment();
        Bundle args = new Bundle();
        args.putString(USER_ID, userId);
        args.putString(FACE_ID, faceId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public FamilyViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(FamilyViewModel.class);
    }

    public void updateUser(String userId,String faceId) {
        if (TextUtils.isEmpty(userId)) return;
        this.userId = userId;
        this.faceId = faceId;
        viewModel.getPhysicalExamData(userId,faceId);
    }

    @Override
    public void initData() {
        super.initData();
        if (getArguments() != null) {
            userId = getArguments().getString(USER_ID);
            faceId = getArguments().getString(FACE_ID);
            updateUser(userId,faceId);
        }
        initRecyclerView();
    }

    private void initRecyclerView() {
        physicalExamRecordAdapter = new PhysicalExamRecordAdapter(R.layout.physical_exam_record_item);
        binding.setLayoutManager(new LinearLayoutManager(getActivity()));
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.divider_transparent_2));
        binding.rvPhysicalExam.addItemDecoration(divider);
        View headerView = LayoutInflater.from(getContext()).inflate(R.layout.physical_exam_record_head, null);
        physicalExamRecordAdapter.addHeaderView(headerView);
        binding.setAdapter(physicalExamRecordAdapter);
        physicalExamRecordAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            int id = view.getId();
            ExaminationReportBean.RowsBean item = (ExaminationReportBean.RowsBean) adapter.getItem(position);
            if (item == null) return;
            if (id == R.id.tv_view_record) {
                Intent intent = new Intent(getActivity(), DetectionResultActivity.class);
                intent.putExtra("reportId", item.getId());
                intent.putExtra("faceId", faceId);
                startActivity(intent);
            } else if (id == R.id.tv_delete_record) {
                new CenterDialog(getContext(), getString(R.string.delete_record_hint),
                        () -> viewModel.deleteReport(item.getId())).show();
            }
        });

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.physicalExamData.observe(this, list -> physicalExamRecordAdapter.setNewData(list));

        LiveEventBus.get(DELETE_REPORT, Boolean.class).observe(this, aBoolean ->
                viewModel.getPhysicalExamData(userId,faceId));
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_physical_exam_record;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
